package com.atguigu.flink0624.chapter07.state;

import org.apache.flink.api.common.state.BroadcastState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.state.ReadOnlyBroadcastState;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.BroadcastStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.BroadcastProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/11/15 15:25
 */
public class Flink03_Operator_BroadCastState {
    
    public static void main(String[] args) {
        // 需要两个流:  数据流  控制流
        // 使用控制流中的数据类控制数据流中的数据的处理方式
        // 把控制流做成广播流 与数据流进行 connect
        // 然后把控制流的数据放入到广播状态, 数据流就可以从广播状态读取数据
        
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        
        DataStreamSource<String> dataStream = env.socketTextStream("hadoop162", 8888);
        DataStreamSource<String> controlStream = env.socketTextStream("hadoop162", 9999);
        MapStateDescriptor<String, String> mapDesc = new MapStateDescriptor<>("controlState", String.class, String.class);
        // 1. 先把控制流做成广播流
        BroadcastStream<String> bcStream = controlStream.broadcast(mapDesc);
        
        // 2. 数据流和广播流进行connect
        dataStream
            .connect(bcStream)
            .process(new BroadcastProcessFunction<String, String, String>() {
                // 处理数据流的元素
                @Override
                public void processElement(String value,
                                           ReadOnlyContext ctx,
                                           Collector<String> out) throws Exception {
                    // 从广播状态读取需要的数据
                    ReadOnlyBroadcastState<String, String> bcState = ctx.getBroadcastState(mapDesc);
                
                    String s = bcState.get("switch");
                
                    if ("1".equals(s)) {
                        //
                        out.collect("使用1号逻辑");
                    } else if ("2".equals(s)) {
                        out.collect("使用2号逻辑");
                    } else {
                    
                        out.collect("使用默认逻辑");
                    }
                
                }
            
                // 处理广播流中的元素
                @Override
                public void processBroadcastElement(String value,
                                                    Context ctx,
                                                    Collector<String> out) throws Exception {
                    // 把广播流中的数据, 写入到广播状态中
                    BroadcastState<String, String> bcState = ctx.getBroadcastState(mapDesc);
                    bcState.put("switch", value);
                
                }
            })
            .print();
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
}
